<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="200px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="供应商名称" >
              <el-input v-model="form.supply_name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联分公司">
              <!-- <el-input v-model="form.company_name" clearable /> -->
              <el-select v-model="form.company_name" clearable>
              <el-option
                v-for="item in isVOption"
                :key="item.id"
                :label="item.nick"
                :value="item.id"
              ></el-option>
            </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4" :offset="4">
            <el-button type="primary" @click="searchList()">查询</el-button>
            <el-button type="primary" @click="toRoutes('supplierAdd')">添加</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed type="index" label="序号" width="80px" align="center" />
        <el-table-column prop="name" label="供应商名称" align="center" />
        <el-table-column prop="company" label="关联分公司" align="center">
          <template slot-scope="scope">{{ scope.row.company.nick }}</template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" align="center">
          <template slot-scope="scope">{{ scope.row.created_at | dates }}</template>
        </el-table-column>
        <el-table-column prop="master_name" label="负责人姓名" align="center" />
        <el-table-column prop="master_phone" label="负责人手机号" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.master_phone | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="发货地址" align="center">
          <template slot-scope="scope">{{scope.row.province.name+scope.row.city.name +scope.row.address }}</template>
        </el-table-column>
        <el-table-column prop="supply_goods_count" label="礼包数" align="center"></el-table-column>
        <el-table-column prop="supply_goods_total" label="累计销量" align="center"></el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <!-- 查看按钮 -->
            <el-button type="text" size="mini" @click="toRoutes('supplierEdit',scope.row.id)">编辑</el-button>
            <!-- 审核按钮 -->
            <el-button type="text" size="mini" @click="deteles(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { supplierList , delSupplier , catList} from "@/api/supplierMange";
export default {
  data() {
    return {
      tableLoading: false,
      // 提交form
      form: {
        page: 1,
        pagesize: 10,
        company_name:'',
        supply_name:''
      },
      isVOption:[],
      // 主页列表显示
      tableData: [],
      // 分页
      page_sizes: [10, 30, 50, 100],
      page_totals: 60
    };
  },
  filters:{
    dates(e){
      return new Date(e*1000).format('yyyy-MM-dd hh:mm:ss')
    }
  },
  mounted() {
    this.getCompanyList();
    this.getList(this.form);
  },
  methods: {
     //获取分公司列表
    getCompanyList() {
      catList()
        .then(res => {
         if(res.data.code==200){
          this.isVOption = res.data.data;
         }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 获取列表
    getList(e) {
      this.tableLoading = true;
      supplierList(e)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data.data;
            this.page_totals = res.data.data.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.message,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
    },
    // 搜索
    searchList() {
      this.form.page = 1;
      this.getList(this.form);
    },
    //删除
    deteles(id) {
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delSupplier({id:id}).then(res=>{
            if(res.data.code==200){
              this.getList(this.form)
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 警告状态弹窗
    warningStatus(txt) {
      this.eleMsg(txt, "warning");
    },
    // 路由跳转
    toRoutes(e, Id) {
      const route = { name: e, query: { id: Id } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
/* .el-range-editor--medium.el-input__inner {
  width: 236px;
} */
.switchBtn {
  color: #ff4949;
}
.alignRight {
  text-align: right;
}
</style>























